package com.shop2cn.iapi.agentproducts.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AddConditionRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.EditConditionRebateBean;
import com.shop2cn.iapi.agentproducts.service.EditConditionRebateCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebate;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateExample;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValue;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValueExample;
/**
*修改返佣条件Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_EditConditionRebate{
	private static EditConditionRebateBean editconditionrebateBean;
	private static EditConditionRebateCall editconditionrebateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改返佣条件Test");
	}

	@Before
	public void caseUp() {
		editconditionrebateBean=new EditConditionRebateBean();
		editconditionrebateCall=new EditConditionRebateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(editconditionrebateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	/**
	 * 修改，验证两张表
	 * @param bean
	 */
	private void checkConditionRebate(EditConditionRebateBean bean,int RebateId){
		//旧策略被删除
		Ymt_ConditionRebateExample example=new Ymt_ConditionRebateExample();
		example.createCriteria()
		.andConditionRebateIdEqualTo(bean.getConditionRebateId()).andActionEqualTo(-1);
		Ymt_ConditionRebate ymt_ConditionRebate1 = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateExample(example).get(0);
		System.out.println(FormatUtil.beanToGSONString(ymt_ConditionRebate1));
		Logger.verifyEquals(ymt_ConditionRebate1.getAction(),-1,"旧的策略被删除");
		
		
		//新的策略验证是否正确
		Ymt_ConditionRebateExample example2=new Ymt_ConditionRebateExample();
		example2.createCriteria()
		.andConditionRebateIdEqualTo(RebateId).andActionEqualTo(0);
		Ymt_ConditionRebate ymt_ConditionRebate = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateExample(example2).get(0);
		System.out.println(FormatUtil.beanToGSONString(ymt_ConditionRebate));
		Logger.verifyEquals(bean.getUserId(),ymt_ConditionRebate.getUserId(), "验证userid");
		Logger.verifyEquals(bean.getBrandIddatabase(),ymt_ConditionRebate.getBrandIdList(), "验证brandid");
		Logger.verifyEquals(bean.getFirstCategoryIddatabase(),ymt_ConditionRebate.getFirstCategoryIdList(), "验证FirstCategoryId");
		Logger.verifyEquals(bean.getDepotIddatabase(),ymt_ConditionRebate.getDepotIdList(), "验证DepotId");
		//验证ConditionType  根据设置的条件，会有不同的优先级
		int brands = bean.getBrandIdList()==null?0:bean.getBrandIdList().size()==0?0:1;//品牌
		int firsts = bean.getFirstCategoryIdList()==null?0:bean.getFirstCategoryIdList().size()==0?0:1;//品类
		int depots = bean.getDepotIdList()==null?0:bean.getDepotIdList().size()==0?0:1;//分仓
		int ConditionType = 0;
		
		if(brands!=0&&firsts!=0&&depots!=0){
			ConditionType=1;
		}else if (brands!=0&&firsts!=0&&depots==0) {
			ConditionType=2;
		}else if (brands!=0&&firsts==0&&depots!=0) {
			ConditionType=3;
		}else if (brands==0&&firsts!=0&&depots!=0) {
			ConditionType=4;
		}else if (brands!=0&&firsts==0&&depots==0) {
			ConditionType=5;
		}else if (brands==0&&firsts!=0&&depots==0) {
			ConditionType=6;
		}else if (brands==0&&firsts==0&&depots!=0) {
			ConditionType=7;
		}else {
			ConditionType=0;
		}
		
		Logger.verifyEquals(ConditionType,ymt_ConditionRebate.getConditionType() ,"验证ConditionType"+brands+firsts+depots);
		Logger.verifyEquals(0,ymt_ConditionRebate.getAction(), "验证action");
		Logger.verifyEquals(RebateId,ymt_ConditionRebate.getConditionRebateId(), "验证action");
		bean.getCatalogRebateList().forEach(b->{
			Ymt_ConditionRebateValueExample example3=new Ymt_ConditionRebateValueExample();
			example3.createCriteria()
			.andAgentTypeIdEqualTo(b.getAgentTypeId())
			.andConditionRebateIdEqualTo(RebateId);
			Ymt_ConditionRebateValue ymt_ConditionRebateValue = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateValueExample(example3).get(0);
			Logger.verifyEquals(RebateId, ymt_ConditionRebateValue.getConditionRebateId(),"验证ConditionRebateId");
			Logger.verifyEquals(b.getAgentTypeId(),ymt_ConditionRebateValue.getAgentTypeId(), "验证AgentTypeId");
			Logger.verifyEquals(b.getRebateValue().doubleValue(),ymt_ConditionRebateValue.getRebateValue().doubleValue(), "验证RebateValue");
			Logger.verifyEquals(ymt_ConditionRebateValue.getRebateType(),1, "验证RebateType");
			Logger.verifyEquals(ymt_ConditionRebateValue.getAction(),0, "验证RebateType");
		});
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditConditionRebate_001() {
		Logger.start(true,"修改佣金");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			editconditionrebateBean = AddAgentProductCallService.InitEditCondictionRebate(RebateId, initAddCondictionRebate);
			editconditionrebateBean.getCatalogRebateList().get(0).setRebateValue(new BigDecimal(22));
			editconditionrebateCall.setData(editconditionrebateBean);
			editconditionrebateCall.callService();		
			Logger.verifyEquals("200", editconditionrebateCall.getString("code"), "验证code");
			checkConditionRebate(editconditionrebateBean,editconditionrebateCall.getRebateId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditConditionRebate_002() {
		Logger.start(true,"修改条件，判断权重 将权重从5变成2，然后验证数据库");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean, 5);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			
			
			editconditionrebateBean = AddAgentProductCallService.InitEditCondictionRebate(RebateId, initAddCondictionRebate);
			editconditionrebateBean.setFirstCategoryIdList(Arrays.asList(addAgentProductBean.getThirdCategoryId()));
			editconditionrebateCall.setData(editconditionrebateBean);
			editconditionrebateCall.callService();			
			Logger.verifyEquals("200", editconditionrebateCall.getString("code"), "验证code");
			
			checkConditionRebate(editconditionrebateBean,editconditionrebateCall.getRebateId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditConditionRebate_003() {
		Logger.start(true,"修改条件，有相同的权重条件，失败");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean, 5);
			initAddCondictionRebate.setBrandIdList(Arrays.asList(1,2));
			initAddCondictionRebate.setFirstCategoryIdList(Arrays.asList(1,2));
			initAddCondictionRebate.setDepotIdList(Arrays.asList(1,2));
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			AddConditionRebateBean initAddCondictionRebate1 = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean, 5);
			initAddCondictionRebate1.setBrandIdList(Arrays.asList(1,2));
			initAddCondictionRebate1.setFirstCategoryIdList(Arrays.asList(3,4));
			initAddCondictionRebate1.setDepotIdList(Arrays.asList(1,2));
			int RebateId1 = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate1);
			editconditionrebateBean = AddAgentProductCallService.InitEditCondictionRebate(RebateId1, initAddCondictionRebate1);
			editconditionrebateBean.setFirstCategoryIdList(Arrays.asList(3,4,1));
			editconditionrebateCall.setData(editconditionrebateBean);
			editconditionrebateCall.callService();			
			Logger.verifyEquals("201", editconditionrebateCall.getString("code"), "验证code");
			Logger.verifyIsContains("已存在相同策略优先级", editconditionrebateCall.getString("message"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
}